package com.jbinfo.mongoot.util;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * @author liuxiaoxi
 * @time 2017年3月26日
 * @describe 运用反射判断从前台传过来的对象哪些属性有值，对其进行赋值
 */
public class XiMethodUtil {

	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日23:23:21
	 * @param obj
	 * @return DBObject
	 * @describe 判断有值进行赋值
	 */
	@SuppressWarnings({ "rawtypes"})
	public static DBObject method(Object obj) throws ParseException {
		DBObject query = new BasicDBObject();
	    try {  
	        Class clazz = obj.getClass();  
	        Field[] fields = obj.getClass().getDeclaredFields();//获得属性  
	        for (Field field : fields) {  
	        	if (field.getName()=="serialVersionUID") {
					continue;
				}
	            PropertyDescriptor pd = new PropertyDescriptor(field.getName(),clazz);  
	            Method getMethod = pd.getReadMethod();//获得get方法  
	            Object o = getMethod.invoke(obj);//执行get方法返回一个Object 
	            Type type = field.getGenericType();
	            String typename=type.toString();
	            if (o!=null&&o!="") {//判断属性的类型，赋对应类型的值
	            	if (typename.equals("class java.lang.String")) {
	            		query.put(field.getName(), o.toString());
					}else if (typename.equals("int")) {
						query.put(field.getName(), new Integer(o.toString()));
					}else if (typename.equals("class java.util.Date")) {
						SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
						query.put(field.getName(), sdf.parse(sdf.format(o)));
					}else if (typename.equals("class java.lang.Long")) {
						query.put(field.getName(), Long.parseLong(o.toString()));
					}
				}
	        }  
	    } catch (SecurityException e) {  
	        e.printStackTrace();  
	    } catch (IllegalArgumentException e) {  
	        e.printStackTrace();  
	    } catch (IntrospectionException e) {  
	        e.printStackTrace();  
	    } catch (IllegalAccessException e) {  
	        e.printStackTrace();  
	    } catch (InvocationTargetException e) {  
	        e.printStackTrace();  
	    }  
	    return query;
	}  
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日23:23:21
	 * @param obj
	 * @return DBObject
	 * @describe 判断有值进行赋值
	 */
	@SuppressWarnings({ "rawtypes"})
	public static DBObject likeMethod(Object obj) throws ParseException {
		DBObject query = new BasicDBObject();
	    try {  
	        Class clazz = obj.getClass();  
	        Field[] fields = obj.getClass().getDeclaredFields();//获得属性  
	        for (Field field : fields) {  
	        	if (field.getName()=="serialVersionUID") {
					continue;
				}
	            PropertyDescriptor pd = new PropertyDescriptor(field.getName(),clazz);  
	            Method getMethod = pd.getReadMethod();//获得get方法  
	            Object o = getMethod.invoke(obj);//执行get方法返回一个Object 
	            Type type = field.getGenericType();
	            String typename=type.toString();
	            DBObject tiaojian =new BasicDBObject();
	            if (o!=null&&o!="") {//判断属性的类型，赋对应类型的值
	            	if (typename.equals("class java.lang.String")) {
	            		tiaojian.put("$regex",o.toString());
	            		if (o.toString().length()>0) {
	            			query.put(field.getName(), tiaojian);
						}
					}else if (typename.equals("int")) {
						query.put(field.getName(), new Integer(o.toString()));
					}else if (typename.equals("class java.util.Date")) {
						SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
						query.put(field.getName(), sdf.parse(sdf.format(o)));
					}else if (typename.equals("class java.lang.Long")) {
						query.put(field.getName(), Long.parseLong(o.toString()));
					}
				}
	        }  
	    } catch (SecurityException e) {  
	        e.printStackTrace();  
	    } catch (IllegalArgumentException e) {  
	        e.printStackTrace();  
	    } catch (IntrospectionException e) {  
	        e.printStackTrace();  
	    } catch (IllegalAccessException e) {  
	        e.printStackTrace();  
	    } catch (InvocationTargetException e) {  
	        e.printStackTrace();  
	    }  
	    return query;
	}  
	
	/**
	 * @author liuxiaoxi
	 * @time 2017年3月25日23:23:21
	 * @param obj
	 * @return DBObject
	 * @describe 判断有值进行赋值
	 */
	@SuppressWarnings({ "rawtypes"})
	public static DBObject method(Object obj,DBObject query) throws ParseException {
	    try {  
	        Class clazz = obj.getClass();  
	        Field[] fields = obj.getClass().getDeclaredFields();//获得属性  
	        for (Field field : fields) {  
	        	if (field.getName()=="serialVersionUID") {
					continue;
				}
	            PropertyDescriptor pd = new PropertyDescriptor(field.getName(),clazz);  
	            Method getMethod = pd.getReadMethod();//获得get方法  
	            Object o = getMethod.invoke(obj);//执行get方法返回一个Object 
	            Type type = field.getGenericType();
	            String typename=type.toString();
	            if (o!=null&&o!="") {//判断属性的类型，赋对应类型的值
	            	if (typename.equals("class java.lang.String")) {
	            		query.put(field.getName(), o.toString());
					}else if (typename.equals("int")) {
						query.put(field.getName(), new Integer(o.toString()));
					}else if (typename.equals("class java.util.Date")) {
						SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
						query.put(field.getName(), sdf.parse(sdf.format(o)));
					}else if (typename.equals("class java.lang.Long")) {
						query.put(field.getName(), Long.parseLong(o.toString()));
					}
				}
	        }  
	    } catch (SecurityException e) {  
	        e.printStackTrace();  
	    } catch (IllegalArgumentException e) {  
	        e.printStackTrace();  
	    } catch (IntrospectionException e) {  
	        e.printStackTrace();  
	    } catch (IllegalAccessException e) {  
	        e.printStackTrace();  
	    } catch (InvocationTargetException e) {  
	        e.printStackTrace();  
	    }  
	    return query;
	}  	
}
